package menu

import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
	"qycms/controllers"
	"qycms/models/auth"
	"qycms/untils"
)

type MenuController struct {
	controllers.BaseController
}
//菜单列表页面
func (this *MenuController) Index()  {
    this.TplName = "menu/index.html"
}
//菜单列表数据
func (this *MenuController) MenuList(){
	//分页计算
	currentPage,err := this.GetInt("page",1)
	pagePerNum,_ := this.GetInt("limit",20)
	if(err != nil ){
		currentPage = 1
	}
	offsetNum := pagePerNum * (currentPage - 1)

	//数据查询
	var count int64 = 0
	menu := []auth.Auth{}

	O := orm.NewOrm()
	q := O.QueryTable(new(auth.Auth))

	keyword := this.GetString("key")
	if keyword != ""{
		q = q.Filter("auth_name__icontains",keyword)
	}

	count,_ = q.Filter("is_delete",0).Count()
	q.Filter("is_delete",0).Limit(pagePerNum).Offset(offsetNum).OrderBy("-create_time").RelatedSel().All(&menu)

	this.Data["json"] = untils.ListJson(count,menu)
	this.ServeJSON()
	this.StopRun()
}
//新建菜单页面
func (this *MenuController) AddMenu()  {
	o := orm.NewOrm()
	place := []auth.Auth{}
	o.QueryTable(new(auth.Auth)).Filter("is_active",1).Filter("is_delete",0).All(&place)
	this.Data["place"] = place
	this.TplName = "menu/add.html"
}
//新建菜单操作
func (this *MenuController) DoAddMenu(){
	//接收参数
	pid,_ := this.GetInt("pid")
	auth_name := this.GetString("auth_name")
	url_for := this.GetString("url_for")
	weight,_:= this.GetInt("weight")
	is_active,_ := this.GetInt("is_active")
	desc := this.GetString("desc")

	//数据验证
	valid := validation.Validation{}
	valid.Required(auth_name,"auth_name").Message("分组名称不能为空")
	valid.Required(url_for,"url_for").Message("地址不能为空")

	//绑定到模型
	o := orm.NewOrm()
	menu := auth.Auth{
		AuthName: auth_name,
		UrlFor: url_for,
		Pid: pid,
		Desc: desc,
		IsActive: is_active,
		Weight: weight,

	}

	_ ,err := o.Insert(&menu)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

//修改菜单页面
func (this *MenuController) EditMenu()  {
	id,_ := this.GetInt("id")
	o := orm.NewOrm()

	menu := []auth.Auth{}
	o.QueryTable(new(auth.Auth)).Filter("is_active",1).Filter("is_delete",0).All(&menu)

	info := auth.Auth{}
	o.QueryTable(new(auth.Auth)).Filter("id",id).Filter("is_delete",0).One(&info)

	this.Data["place"] = menu
	this.Data["info"] = info
	this.TplName = "menu/edit.html"
}
//修改菜单操作
func (this *MenuController) UpdatedMenu(){
	//接收参数
	ID := this.GetString("Id")
	pid,_ := this.GetInt("pid")
	auth_name := this.GetString("auth_name")
	url_for := this.GetString("url_for")
	weight,_:= this.GetInt("weight")
	is_active := this.GetString("is_active")
	desc := this.GetString("desc")

	//数据验证
	valid := validation.Validation{}
	valid.Required(auth_name,"auth_name").Message("分组名称不能为空")
	valid.Required(url_for,"url_for").Message("地址不能为空")

	//绑定到模型
	var new_is_active int
	if is_active == "on"{
		new_is_active = 1
	}else{
		new_is_active = 0
	}

	//绑定到模型
	o := orm.NewOrm()
	menu := orm.Params{
		"auth_name": auth_name,
		"url_for": url_for,
		"pid": pid,
		"desc": desc,
		"is_active": new_is_active,
		"weight": weight,

	}

	_ ,err := o.QueryTable(new(auth.Auth)).Filter("id",ID).Update(menu)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

//删除菜单操作
func (this *MenuController) DelMenu(){
	//接收参数
	id,_ := this.GetInt("id");
	o := orm.NewOrm()


	//查询是否有子集
	hasChild,_ := o.QueryTable(new(auth.Auth)).Filter("pid",id).Filter("is_delete",0).Count()
	if(hasChild > 0){
		this.Err(2001,"请先删除子集",nil)
	}

	//数据更新

	o.QueryTable(new(auth.Auth)).Filter("id",id).Update(orm.Params{
		"is_delete":1,
	})

	this.Suc(nil)
}